//
//  TravelStrokeDetailsViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelStrokeDetailsViewController.h"
#import "DataAnalysis.h"
#import "TravelStrokeBigModel.h"
#import "TravelStrokeOneTableViewCell.h"
#import "TravelStrokeTwoTableViewCell.h"
#import "TravelTopicDetailsOneTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>



@interface TravelStrokeDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *arrLoading;

@property (nonatomic, retain) UIView *titleView;

@property (nonatomic, retain) TravelStrokeBigModel *bigModel;

@property (nonatomic, retain) NSDictionary *dic;



@end

@implementation TravelStrokeDetailsViewController
-(void)dealloc
{
    [_tableView release];
    [_arrLoading release];
     [_titleView release];
    [_bigModel release];
    [_dic release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self loading];
    
    [self createTableView];
//    
    [self createTitle];
    // Do any additional setup after loading the view.
}


#pragma mark 网络请求
-(void)loading
{
    [DataAnalysis dataAnalysisGETWithUrlString:[NSString stringWithFormat:@"http://chanyouji.com/api/plans/%@.json?page=1", self.idNumber] parameters:nil showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
   
        if (responseObject != nil) {
            self.bigModel= [TravelStrokeBigModel travelStrokeBigModelWithNSDictionary:responseObject];
        }
                [self.tableView reloadData];
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@", error);
        
    }];
    
    
}





#pragma mark 创建tableView
-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 45 *kHeight, self.view.frame.size.width, self.view.frame.size.height - 45 *kHeight) style:UITableViewStyleGrouped];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsVerticalScrollIndicator = NO;

    self.tableView.sectionFooterHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[TravelStrokeOneTableViewCell class] forCellReuseIdentifier:@"TravelStrokeOneTableViewCell"];
    [self.tableView registerClass:[TravelStrokeTwoTableViewCell class] forCellReuseIdentifier:@"TravelStrokeTwoTableViewCell"];
    
    [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 200 *kHeight;
    }else{
        return 40*kHeight;
    }
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  
   
    return [self.bigModel.arr[section] arrg].count;
   
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.bigModel.arr.count;
    
}
//
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        CGFloat frame = [TravelTopicDetailsOneTableViewCell heightOfLabel:[self.bigModel.arr[indexPath.section] memo] andTextFont:[UIFont systemFontOfSize:15 *kWidth] andTextWidth:self.view.frame.size.width - 20 * kWidth];
        
        return frame + 50 *kHeight;
    }else
    {
        CGFloat frameT =[TravelTopicDetailsOneTableViewCell heightOfLabel:[[self.bigModel.arr[indexPath.section] arrg][indexPath.row - 1] tips] andTextFont:[UIFont systemFontOfSize:14 *kWidth] andTextWidth:self.view.frame.size.width - 20 * kWidth];
        
        
        return frameT + 210*kHeight;
    }
    
    
    
}


#pragma mark 创建分区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 *kHeight)];
        UIImageView *imagePic = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 160*kHeight)];
        
        [imagePic sd_setImageWithURL:[NSURL URLWithString:self.bigModel.image_url] placeholderImage:[UIImage imageNamed:@"Travel_place"]];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, imagePic.frame.size.height / 3 *2, imagePic.frame.size.width, 20*kHeight)];
        
        label.font =[ UIFont systemFontOfSize:20 *kWidth];
        label.text = self.bigModel.name ;
        label.textColor = [UIColor whiteColor];
        label.textAlignment =NSTextAlignmentCenter;
        [imagePic addSubview:label];
        
        [view addSubview:imagePic];
        
        UILabel *LabelDay =[[UILabel alloc]initWithFrame:CGRectMake(0, 160*kHeight, self.view.frame.size.width, 40*kWidth)];
        
        LabelDay.font = [UIFont systemFontOfSize:17 *kWidth];
        LabelDay.textAlignment = NSTextAlignmentCenter;
        LabelDay.textColor = [UIColor blackColor];
        LabelDay.backgroundColor = [UIColor colorWithRed:0.953 green:0.952 blue:0.973 alpha:1.000];
        LabelDay.text = [NSString stringWithFormat:@"Day%ld",(section + 1)];
        [view addSubview:LabelDay];
        
        return view;
    }else{
    
    
    UILabel *LabelDay =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40*kWidth)];
    
    LabelDay.font = [UIFont systemFontOfSize:17 *kWidth];
    LabelDay.textAlignment = NSTextAlignmentCenter;
    LabelDay.textColor = [UIColor blackColor];
    LabelDay.backgroundColor = [UIColor colorWithRed:0.953 green:0.952 blue:0.973 alpha:1.000];
    LabelDay.text = [NSString stringWithFormat:@"Day%ld",(section + 1)];
    

    
             return LabelDay;
    }
}



//
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row == 0) {
        
        TravelStrokeOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelStrokeOneTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
              cell.model = self.bigModel.arr[indexPath.section];
        
        return cell;
    }else{
    
    TravelStrokeTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelStrokeTwoTableViewCell" forIndexPath:indexPath];
    
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    cell.model = [self.bigModel.arr[indexPath.section ] arrg][indexPath.row -1];
        return cell;
       }

    
}


#pragma mark 创建标题
-(void)createTitle
{
   
    
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,kHeight * 64)];
    self.titleView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.titleView];
    [self.titleView release];
    [_titleView release];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(15 * kWidth, 25 * kHeight, 20 * kWidth, 20 * kHeight);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    [self.titleView addSubview:backBtn];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30 * kWidth, self.titleView.frame.size.height / 2 - 10 * kHeight, kWidth * 60, kHeight * 30)];
    
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text =  @"行程";
    titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    [self.titleView addSubview:titleLabel];
    [titleLabel release];
}

//返回点击事件
-(void)backAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
